//void, Obj This
Hero Leader;
Unit u;
Settlement this, sett_target;
Building bld;
Query qOllMil;
int min_resource_food, min_resource_gold, i, j, p, licznik, hdbp, nMil;
ObjList olUnitsTributeOut, ol_mil, olhero, olCatap, sel, olTW;   
Wagon wagon;
str target_name, horde;

Sleep(rand(1000)+500);
this = This.AsBuilding().settlement;
bld = This.AsBuilding();

EnvWriteInt(bld.player, "RACE", bld.race);
if(EnvReadInt(this, "AssignName") == 0){
	Building build;
	str names;
	
	EnvWriteInt(this, "AssignName", 1);
	
	if(.player < 9){
		EnvWriteInt(this, "rev", 287);
		pr("Player "+.player+" - GBR Tactics v.2.4 r"+EnvReadInt(this, "rev"));
	}
	
	if(bld.name == ""){
		names = "B_"+bld.class+"_"+bld.player+bld.race+rand(99)+rand(99);
		build = GetNamedObj(names).obj.AsBuilding;
		if(!build.IsValid){
			bld.SetName("");       
			bld.SetName(names);
		} else {
			names = "B_"+bld.class+"_"+bld.player+bld.race+rand(99)+rand(99);
			bld.SetName("");       
			bld.SetName(names);
		}
	}
}

p = .player;
min_resource_food = GetConst("MinResQtyToTransportFood");
min_resource_gold = GetConst("MinResQtyToTransportGold");
hdbp = GetConst("HealthDropBoundPercent");

while (1) {
	
	if(p != .player){

		EnvWriteString(this, "tribute_food", "off");
		EnvWriteString(this, "tribute_gold", "off");
		EnvWriteString(this, "tribute_people", "off");
				
		EnvWriteString(this, "stoptribute_food", "on");
		EnvWriteString(this, "stoptribute_gold", "on");
		EnvWriteString(this, "stoptribute_people", "on");
		.StopSupply();
		break;
	}
	olTW = ClassPlayerObjs("BaseTownhall", .player).GetObjList();
	licznik+=1;
	
	if(.player != 15)
	if(olTW[0].AsBuilding() == bld){
		if(IsMultiplayer())
			if(GetSpeed() > 1300)
				SetSpeed(1300);
			
		if(EnvReadInt("Editor") == 0 && licznik >= 70){
			licznik = 0;
			
			nMil = MilUnits(.player);
			if(nMil < 300){
				if(nMil != EnvReadInt(.player, "army/CountInt"))
					EnvWriteInt(.player, "army/CountInt", nMil);
					if(EnvReadString(.player, "army/CountStr") == "H!")
						EnvWriteString(.player, "army/CountStr", "");
			} else {
				if(nMil != EnvReadInt(.player, "army/CountInt")){
					EnvWriteInt(.player, "army/CountInt", nMil);
					if(EnvReadString(.player, "army/CountStr") != "H!")
						EnvWriteString(.player, "army/CountStr", "H!");
				}
			}
			horde = EnvReadString(.player, "army/CountStr");
			if(nMil >= 10){
				
				//pr("for start");
				//ol_mil = GetPlayerUnits(.player, "Military");
				qOllMil = ClassPlayerObjs("Military", .player);
				qOllMil = Subtract(qOllMil, ClassPlayerObjs("Sentry", .player));
				qOllMil = Subtract(qOllMil, ClassPlayerObjs("Hero", .player));
				qOllMil = Subtract(qOllMil, ClassPlayerObjs("ShipBattle", .player));
				ol_mil= qOllMil.GetObjList;
				//pr(ol_mil.count);
				for(j=0; j<ol_mil.count; j+=1){
					u = ol_mil[j].AsUnit;
					if(u.IsValid)
					if(u.IsAlive){
						if(horde == "H!"){
							if(!u.HasItem("Status of the Horde!"))
								if(u.item_count < u.max_items)
									u.AddItem("Status of the Horde!");
						} else {
							if(u.HasItem("Status of the Horde!"))
								u.RemoveItemsOfType("Status of the Horde!");
						}
						if(rand(100) < 52)
						if(!u.IsHeirOf("LightInfantry")){
							if(u.food == 0 && u.health <= (u.maxhealth*hdbp/100)){
								if(u.player != 15){
									if(u.InHolder){
										if(u.GetHolderSett.food <= 0){
											if(u.GetHolderSett.player != 15){
												if(!u.GetHolderSett.GetCentralBuilding.IsHeirOf("BaseTownhall")){
													UserNotification("building attacked", "", u.posRH, u.player); 
													u.GetHolderSett.SetPlayer(15);
												}
											}
										}
									}
									u.SetPlayer(15);
									u.SetFood(1);
									u.SetFeeding( false );
									u.SetEntering( false );
								}
							}
						}
					}
					Sleep(50);
				}
			}
		}
	}
	if (.supplied == this)
		.StopSupply();
	//if (!.supplied.IsValid()) continue;
	Sleep(1000);
	if(EnvReadString(this, "tribute_gold") == "on"){
		
		target_name = EnvReadString(this, "tribute_gold_target");
		sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;
		if(sett_target.IsValid){
			if(.IsEnemy(sett_target.player)) {
				EnvWriteString(this, "tribute_gold", "off");
				EnvWriteString(this, "stoptribute_gold", "on");
				continue;
			}
			if (.gold > min_resource_gold) {
				wagon = .CreateMuleGold(1000);
				if(wagon.IsValid){
					wagon.SetCommand("unload", sett_target.GetCentralBuilding());
					wagon.AddCommand(false, "unload", .GetCentralBuilding());
				}
			}
			sel = _GetSelection().ObjAlly(.player).ObjClass("BaseTownhall");
			if(sel.count == 1){
				Building tw;
				
				tw = sel[0].AsBuilding;
				if(tw.IsValid){
					if(tw == .GetCentralBuilding){
						//UserNotification("tribute people", "", Point(target_x, target_y), .player); 
						UserNotification("tribute gold", "", sett_target.GetCentralBuilding.pos, .player); 
						//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
					}
				}
			}
		}
	}
    
	if(EnvReadString(this, "tribute_food") == "on"){
				
		target_name = EnvReadString(this, "tribute_food_target");
		sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;
		if(sett_target.IsValid){
			if(.IsEnemy(sett_target.player)) {
				EnvWriteString(this, "tribute_food", "off");
				EnvWriteString(this, "stoptribute_food", "on");
				.StopSupply();
				continue;
			}
			if(.food > min_resource_food) {
				wagon = .CreateMuleFood(1000);
				if(wagon.IsValid){
					wagon.SetCommand("unload", sett_target.GetCentralBuilding());
					wagon.AddCommand(false, "unload", .GetCentralBuilding()); 
				}
			}
			sel = _GetSelection().ObjAlly(.player).ObjClass("BaseTownhall");
			if(sel.count == 1){
				Building tw;
				
				tw = sel[0].AsBuilding;
				if(tw.IsValid){
					if(tw == .GetCentralBuilding){
						//UserNotification("tribute people", "", Point(target_x, target_y), .player); 
						//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
						UserNotification("tribute food", "", sett_target.GetCentralBuilding.pos, .player); 
					}
				}
			}
		}
	}

    Sleep(1000);
	if(EnvReadString(this, "tribute_people") == "on"){
		olUnitsTributeOut.Clear;
		for(i=0; i<.Units.count; i+=1) {
			Unit u;
			u = .Units[i].AsUnit;
			if(!u.IsValid) continue;
			if(u.IsMilitary){
				if( !u.IsHeirOf("SentryOutpost")
				&& !u.IsHeirOf("CaptainOutpost")){
					if(!u.hero.IsValid){
						if(EnvReadString(this, "autotrain") == "on"){
							if(u.level >= EnvReadInt(.player, "maxtrainlevel")){
								if(!u.GetCommanded)
									u.SetCommanded(true);
									
								if(u.IsHeirOf("Hero")){
									if(!u.AsHero.HasArmy)
										olUnitsTributeOut.Add(u);
								} else { 
									olUnitsTributeOut.Add(u);
								}
							}
						} else {
							if(u.IsHeirOf("Hero")){
								if(!u.AsHero.HasArmy)
									olUnitsTributeOut.Add(u);
							} else { 
								olUnitsTributeOut.Add(u);
							}
						}
					}
				}
			}
			if(olUnitsTributeOut.count >= 10) break;
		}

		{
			int target_x, target_y;
			point pos, pp;
			rect rc;
			
			target_name = EnvReadString(this, "tribute_people_target");
			sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;                        
			target_x = EnvReadInt(this, "tribute_people_target_x");
			target_y = EnvReadInt(this, "tribute_people_target_y");
			if(sett_target.IsValid){
				Building bld_target;
				bld_target = sett_target.GetCentralBuilding;
				if(!.IsEnemy(sett_target.player)){
					if(olUnitsTributeOut.count >= 1){
						if(bld_target.IsHeirOf("Outpost") ||
						   bld_target.IsHeirOf("BaseShipyard") ||
						   bld_target.IsHeirOf("IronMine") ||
						   bld_target.IsHeirOf("BaseTownhall")){
						   
							olUnitsTributeOut.SetCommand("enter", bld_target);
								
						} else {
							rc = GetMapRect();
							pos.Set(0, bld_target.sight / 3);
							pos.Rot(rand(360));     pp = pos + bld_target.pos; pp.IntoRect(rc); 
							olUnitsTributeOut.SetCommand("move", pp);
						}
						//Sleep(4*1000);
					}
				} else {
					EnvWriteString(this, "tribute_people", "off");
					EnvWriteString(this, "stoptribute_people", "on");
				}
				sel = _GetSelection().ObjAlly(.player).ObjClass("BaseTownhall");
				if(sel.count == 1){
					Building tw;
					
					tw = sel[0].AsBuilding;
					if(tw.IsValid){
						if(tw == .GetCentralBuilding){
							UserNotification("tribute people", "", bld_target.pos, .player); 
							//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
						}
					}
				}
			} else if(target_x != 0 && target_y != 0){
				if(olUnitsTributeOut.count >= 1){
					rc = GetMapRect();
					pos.Set(0, 200);
					pos.Rot(rand(360));     pp = pos + Point(target_x, target_y); pp.IntoRect(rc); 
					olUnitsTributeOut.SetCommand("move", pp);

					//Sleep(4*1000);
				}
				sel = _GetSelection().ObjAlly(.player).ObjClass("BaseTownhall");
				if(sel.count == 1){
					Building tw;
					
					tw = sel[0].AsBuilding;
					if(tw.IsValid){
						if(tw == .GetCentralBuilding){
							UserNotification("tribute people", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
						}
					}
				}
			}
		}
	} 
	Sleep(1000);

}